package com.taomeng.servlet.admin;

import com.taomeng.servlet.BaseServlet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebServlet("/admin/index/*")
public class AdminIndexServlet extends BaseServlet {
    private static final String adminUri = "/admin/public/";


    private void cls(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("cls", req, resp);
    }
    private void user(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("user", req, resp);
    }

    private void leave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("leave", req, resp);
    }

    private void permission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("permission", req, resp);
    }

    private void role(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("role", req, resp);
    }

    private void student(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("student", req, resp);
    }

    private void department(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        urlForward("department", req, resp);
    }

    private void urlForward(String uri, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(adminUri + uri + ".jsp").forward(req, resp);
    }


    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext application = this.getServletContext();
        // 解决servlet用@Autowired自动注入service失败的问题
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, application);
    }
}
